'use strict';
/**
 * Created by FreezeSoul on 2015/12/9.
 */
define([
        'angular',
        'adConfig',
        'app/modules/player/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.player')
            .factory('adPlayerService', ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function loadPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/load';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function loadChart(chartId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/chart/load';
                    adRequest(url, {
                        chartId: chartId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadDataSet(dataSetCodes, params) {
                    if (!angular.isArray(params)) params = [];
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'index/chart/data';
                    adRequest(url, {
                        dataSetCodes: angular.toJson(dataSetCodes),
                        params: angular.toJson(params)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadUserPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/userpage/loadUserPage';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function addOrUpdatePage(pageId, pageData) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/userpage/addOrUpdate';
                    adRequest(url, {
                        pageId: pageId,
                        pageData: pageData
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function restoreDefaultPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/userpage/restoreDefault';
                    adRequest(url, {
                        pageId: pageId,
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadUserPages() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/userpage/loadUserRolePages';
                    adRequest(url, {
                        resourceType: adConfig.resourceTypePage
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadUserCharts() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/userpage/loadUserRoleCharts';
                    adRequest(url, {
                        folderType: adConfig.chartFolderType,
                        resourceType: adConfig.resourceTypeChart
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                return {
                    loadPage: loadPage,
                    loadChart: loadChart,
                    loadDataSet: loadDataSet,
                    loadUserPage: loadUserPage,
                    loadUserPages: loadUserPages,
                    loadUserCharts: loadUserCharts,
                    addOrUpdatePage: addOrUpdatePage,
                    restoreDefaultPage: restoreDefaultPage
                };
            }]);
    });